/**
 * 学生选课系统 - 主程序
 * 编译时通过 -DINPUT_MODE_FILE 或 -DINPUT_MODE_CLI 决定运行模式
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "Include/student_pub.h"
#include "Include/course_pub.h"
#include "Include/file_pub.h"

// 全局变量声明（在模块中定义）
extern Student students[MAX_STUDENT];
extern int student_count;
extern Course courses[MAX_COURSES];
extern int course_count;

void show_main_menu() {
    printf("\n========== 学生选课系统 ==========\n");
    printf("1. 学生信息管理\n");
    printf("2. 课程信息管理\n");
    printf("3. 选课操作\n");
    printf("4. 退课操作\n");
    printf("5. 显示所有学生\n");
    printf("6. 显示所有课程\n");
    printf("7. 查看课程详情\n");
    printf("0. 退出系统\n");
    printf("请选择操作: ");
}

void student_management_menu() {
    printf("\n--- 学生信息管理 ---\n");
    printf("1. 添加学生\n");
    printf("2. 删除学生\n");
    printf("3. 修改学生信息\n");
    printf("4. 查找学生\n");
    printf("0. 返回主菜单\n");
    printf("请选择: ");
}

void course_management_menu() {
    printf("\n--- 课程信息管理 ---\n");
    printf("1. 添加课程\n");
    printf("2. 删除课程\n");
    printf("3. 修改课程信息\n");
    printf("4. 查看课程详情\n");
    printf("0. 返回主菜单\n");
    printf("请选择: ");
}

void process_student_management() {
    int choice;
    char id[20], name[50];
    
    do {
        student_management_menu();
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                printf("输入学号: "); scanf("%s", id);
                printf("输入姓名: "); scanf("%s", name);
                add_student(id, name);
                log_operation(OP_ADD_STUDENT, id, name);
                break;
            case 2:
                printf("输入要删除的学号: "); scanf("%s", id);
                delete_student(id);
                log_operation(OP_DELETE_STUDENT, id, "删除学生");
                break;
            case 3:
                printf("输入要修改的学号: "); scanf("%s", id);
                printf("输入新姓名: "); scanf("%s", name);
                modify_student(id, name);
                log_operation(OP_MODIFY_STUDENT, id, name);
                break;
            case 4:
                printf("输入要查找的学号: "); scanf("%s", id);
                Student* s = find_student_by_id(id);
                if (s) {
                    printf("找到学生: %s %s\n", s->id, s->name);
                } else {
                    printf("未找到学生\n");
                }
                break;
        }
    } while(choice != 0);
}

void process_course_management() {
    int choice;
    char id[20], name[50], type[20];
    int hours, credits, max;
    
    do {
        course_management_menu();
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                printf("输入课程编号: "); scanf("%s", id);
                printf("输入课程名称: "); scanf("%s", name);
                printf("输入课程性质: "); scanf("%s", type);
                printf("输入授课学时: "); scanf("%d", &hours);
                printf("输入学分: "); scanf("%d", &credits);
                printf("输入人数上限: "); scanf("%d", &max);
                add_course(id, name, type, hours, credits, max);
                log_operation(OP_ADD_COURSE, id, name);
                break;
            case 2:
                printf("输入要删除的课程编号: "); scanf("%s", id);
                delete_course(id);
                log_operation(OP_DELETE_COURSE, id, "删除课程");
                break;
            case 3:
                printf("输入要修改的课程编号: "); scanf("%s", id);
                printf("输入新课程名称: "); scanf("%s", name);
                printf("输入新课程性质: "); scanf("%s", type);
                printf("输入新授课学时: "); scanf("%d", &hours);
                printf("输入新学分: "); scanf("%d", &credits);
                printf("输入新人数上限: "); scanf("%d", &max);
                modify_course_info(id, name, type, hours, credits, max);
                log_operation(OP_MODIFY_COURSE, id, name);
                break;
            case 4:
                printf("输入要查看的课程编号: "); scanf("%s", id);
                show_course_details(id);
                break;
        }
    } while(choice != 0);
}

void process_course_selection() {
    char student_id[20], course_id[20];
    
    printf("\n--- 选课操作 ---\n");
    printf("输入学号: "); scanf("%s", student_id);
    printf("输入课程编号: "); scanf("%s", course_id);
    
    // 查找学生和课程
    Student* student = find_student_by_id(student_id);
    Course* course = find_course_by_id(course_id);
    
    if (student == NULL) {
        printf("错误：未找到学号 %s 的学生\n", student_id);
        return;
    }
    
    if (course == NULL) {
        printf("错误：未找到课程编号 %s 的课程\n", course_id);
        return;
    }
    
    // 检查课程是否已满
    if (course->selected_count >= course->max_students) {
        printf("错误：课程 %s 人数已满(%d/%d)\n", course_id, course->selected_count, course->max_students);
        return;
    }
    
    // 检查学生是否已选该课程
    for (int i = 0; i < student->course_count; i++) {
        if (strcmp(student->selected_courses[i], course_id) == 0) {
            printf("错误：学生 %s 已选课程 %s\n", student_id, course_id);
            return;
        }
    }
    
    // 检查学生选课数量是否已达上限
    if (student->course_count >= MAX_COURSES_PER_STUDENT) {
        printf("错误：学生 %s 选课数量已达上限(%d)\n", student_id, MAX_COURSES_PER_STUDENT);
        return;
    }
    
    // 执行选课
    if (add_course_to_student(student_id, course_id, course->credits) == 0 && 
        add_student_to_course(course_id, student_id) == 0) {
        printf("选课成功！学生 %s 已选课程 %s (学分: %d)\n", student_id, course_id, course->credits);
        log_operation(OP_SELECT_COURSE, student_id, course_id);
    }
}

void process_course_drop() {
    char student_id[20], course_id[20];
    
    printf("\n--- 退课操作 ---\n");
    printf("输入学号: "); scanf("%s", student_id);
    printf("输入课程编号: "); scanf("%s", course_id);
    
    // 查找学生和课程
    Student* student = find_student_by_id(student_id);
    Course* course = find_course_by_id(course_id);
    
    if (student == NULL) {
        printf("错误：未找到学号 %s 的学生\n", student_id);
        return;
    }
    
    if (course == NULL) {
        printf("错误：未找到课程编号 %s 的课程\n", course_id);
        return;
    }
    
    // 执行退课
    if (remove_course_from_student(student_id, course_id) == 0 && 
        remove_student_from_course(course_id, student_id) == 0) {
        printf("退课成功！学生 %s 已退选课程 %s\n", student_id, course_id);
        log_operation(OP_DROP_COURSE, student_id, course_id);
    } else {
        printf("退课失败：学生 %s 未选课程 %s\n", student_id, course_id);
    }
}

int main() {
    // 初始化所有系统
    init_student_system();
    init_course_system();
    init_file_system();
    
    // 根据编译模式选择数据初始化方式
#ifdef INPUT_MODE_FILE
    printf("=== 文件输入模式 ===\n");
    if (load_data_from_files() == 0) {
        printf("数据加载成功！\n");
    } else {
        printf("数据加载失败或文件不存在\n");
    }
#else
    printf("=== 命令行输入模式 ===\n");
    input_data_from_keyboard();
#endif
    
    int choice;
    do {
        show_main_menu();
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                process_student_management();
                break;
            case 2:
                process_course_management();
                break;
            case 3:
                process_course_selection();
                break;
            case 4:
                process_course_drop();
                break;
            case 5:
                list_all_students();
                break;
            case 6:
                list_all_courses();
                break;
            case 7:
                printf("输入课程编号: ");
                char course_id[20];
                scanf("%s", course_id);
                show_course_details(course_id);
                break;
            case 0:
                printf("退出系统...\n");
#ifdef INPUT_MODE_FILE
                save_data_to_files();
                printf("数据已保存到文件\n");
#endif
                break;
            default:
                printf("无效选择！\n");
        }
    } while(choice != 0);
    
    return 0;
}
